package com.hw01.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import com.hw01.utils.DateUtils;
import com.hw01.vo.ClockInVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hw01.common.Result;
import com.hw01.entity.PunchingCardRecord;
import com.hw01.mapper.PunchingCardRecordMapper;
import com.hw01.service.PunchingCardRecordService;
import com.hw01.vo.PunchingCardRecordVo;

/**
 * <p>
 * 打卡记录 服务实现类
 * </p>
 *
 * @author zhong
 * @since 2024-03-18
 */
@Service
public class PunchingCardRecordServiceImpl extends ServiceImpl<PunchingCardRecordMapper, PunchingCardRecord>
    implements PunchingCardRecordService {

    @Autowired
    PunchingCardRecordMapper mapper;

    @Override
    public Result<List<PunchingCardRecordVo>> getPunchingCardRecord(Long userId, String year, String month) {

        if (StringUtils.isBlank(year)) {
            year = DateUtils.getYear();
        }
        month = DateUtils.mendMonth(month);

        String firstDay = year + "-" + month + "-01";

        List<PunchingCardRecordVo> list = mapper.selectPunchingCardRecord(userId, firstDay);

        return Result.success("查询成功", list);
    }

    @Override
    public Result<LocalDateTime> clockIn(Long userId,String address) {
        // 判断今天是否已经打过卡
        Result<ClockInVo> todayClockIn = todayClockIn(userId);
        if (todayClockIn.getData().getIsClockIn()) {
            return Result.error("今日已打卡");
        }

        PunchingCardRecord pcr = new PunchingCardRecord();
        LocalDateTime clockIn = LocalDateTime.now();
        pcr.setClockIn(clockIn);
        pcr.setUserId(userId);
        pcr.setAddress(address);

        mapper.insert(pcr);

        return Result.success("打卡成功", clockIn);
    }

    @Override
    public Result<ClockInVo> todayClockIn(Long userId) {
        ClockInVo vo = new ClockInVo();

        String todayDateStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        PunchingCardRecord todayPcr =
            mapper.selectOne(new LambdaQueryWrapper<PunchingCardRecord>().eq(PunchingCardRecord::getUserId, userId)
                .apply("date_format(clock_in, '%Y-%m-%d') = '" + todayDateStr + "'"));

        if (todayPcr != null) {
            vo.setIsClockIn(Boolean.TRUE);
            vo.setTodayClockIn(todayPcr.getClockIn());
        } else {
            vo.setIsClockIn(Boolean.FALSE);
        }

        String yesterdayDateStr = LocalDate.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        PunchingCardRecord lastPcr =
            mapper.selectOne(new LambdaQueryWrapper<PunchingCardRecord>().eq(PunchingCardRecord::getUserId, userId)
                .apply("date_format(clock_in, '%Y-%m-%d') = '" + yesterdayDateStr + "'"));

        if (lastPcr != null) {
            vo.setLastClockIn(lastPcr.getClockIn());
        }

        return Result.success("今日未打卡", vo);
    }
}
